IHG.Toolbar.Button = function(options)
{
	this.id = options.id || _uniqueID++;
	this.label = options.label || null;
	this.showLabel = options.showLabel || false;
	this.image = options.image || 'none';
	this.shortcut = options.shortcut;
	this.state = IHG.Toolbar.DISABLED;
	this.tag = options.tag || [];
	
	var _self = this;
		
	if(options.action) {
		this.action = options.action;
	}
	
	if(options.condition) {
		this.condition = options.condition;
	}
}

IHG.Toolbar.Button.prototype = {
	action: function(sender)
	{
		if(this.tag === undefined || this.tag.length < 2) {
			throw IHG.BadMethodCall('If no action callback is defined, at least an \
				tag-array with a length of 2 should be provided for button ' + (this.label || this.id));
		}
		
		with(sender.selectionController) {
			if(this.state === IHG.Toolbar.DISABLED) return;
					
			if(isWrapped(this.tag[0], this.tag[1])) {
				unwrap(this.tag[0], this.tag[1]);
			} else {
				mark(this.tag[0], this.tag[1]);
			}
		}
	},
	
	condition: function(sender)
	{
		if(this.tag === undefined || this.tag.length < 2) {
			throw IHG.BadMethodCall('If no condition callback is defined, at least an \
				tag-array with a length of 2 should be provided for button ' + (this.label || this.id));
		}
		
		with(sender.selectionController) {
			if(isMarked('[', ']')
			|| isMarked('[img]',  '[/img]')
			|| isMarked('[code]', '[/code]')
			|| isMarked('[php]',  '[/php]')) {
				this.state = IHG.Toolbar.DISABLED;
			} else if(isMarked(this.tag[0], this.tag[1])) {
				this.state = IHG.Toolbar.ACTIVE;
			} else {
				this.state = IHG.Toolbar.ENABLED;
			}
		}
	},
	
	trigger: function(sender)
	{
		this.action.call(this, sender);
	},
	
	draw: function(sender, container)
	{	
		this.condition.call(this, sender);
				
		if(container) {
			this.listItem = document.createElement('li');
			
			this.listItemContainer = document.createElement('a');
			//this.listItemContainer.title = this.label + (typeof this.shortcut == 'string' ? ' ('+this.shortcut+')' : '');
			this.listItem.appendChild(this.listItemContainer);
			
			this.listButton = document.createElement('img');
			this.listButton.src = this.image;
			
			this.listItemContainer.appendChild(this.listButton);
			
			this.listLabel = document.createElement('span');
			this.listLabel.appendChild(document.createTextNode(''));
			this.listLabel.title = typeof this.shortcut == 'string' ? this.shortcut : '';
			
			this.listItemContainer.appendChild(this.listLabel);
			
			container.appendChild(this.listItem);
		}
		
		if(this.label != this.listLabel.firstChild.nodeValue) {
			this.listLabel.replaceChild(document.createTextNode(this.label), this.listLabel.firstChild);
			this.listItemContainer.title = this.label + (typeof this.shortcut == 'string' ? ' ('+this.shortcut+')' : '');
		}
		
		switch(this.state) {
			case IHG.Toolbar.DISABLED:
				this.listItem.className = this.id + (this.showLabel ? ' labelled': '') + ' disabled';
				break;
			case IHG.Toolbar.ACTIVE:
				this.listItem.className = this.id + (this.showLabel ? ' labelled': '') + ' enabled active';
				break;
			default:
				this.listItem.className = this.id + (this.showLabel ? ' labelled': '') + ' enabled';
				break;
		}
		
		return this.listItem;
	}
}